package keyboard.gui;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.JComponent;

import keyboard.model.Keyboard;
import main.model.Settings;

@SuppressWarnings("serial")
class KeyboardPanel extends JComponent {
	
	public KeyboardPanel(Keyboard keyboard) {
		Settings settings = new Settings();
		File f = new File("config/" + settings.configKeyboard());
		System.out.println("Loaded keyboardlayout: "  + f);
		int size = settings.keySize();
		try {
			Scanner s = new Scanner(f);
			
			while (s.hasNext()) {
				String key = s.next();
				String capsKey = s.next();
				int x = (int) (s.nextDouble()*size);
				int y = (int) (s.nextDouble()*size);
				int width = (int) (s.nextDouble()*size);
				int height = (int) (s.nextDouble()*size);
				
				AbstractKey k = null;

				if (key.equals("_P")) {
					k = new SpaceKey(keyboard, capsKey);
				} else if (key.equals("_E")) {
					k = new EnterKey(keyboard, capsKey);
				} else if (key.equals("_S")) {
					k = new ShiftKey(keyboard, capsKey);
				} else if (key.equals("_T")) {
					k = new TabKey(keyboard, capsKey);
				} else if (key.equals("_B")) {
					k = new BackspaceKey(keyboard, capsKey);
				} else if (key.equals("_C")) {
					k = new CapslockKey(keyboard, capsKey);
				} else {
					k = new NormalKey(keyboard, key, capsKey);
				}
				keyboard.addObserver(k);
				k.setBounds(x,y,width,height);
				add(k);
			}
		} catch (FileNotFoundException e) {
			System.err.println("Kunde inte ladda in configfil för tangentbord");
			e.printStackTrace();
		}
	}
	
	
}
